/*
 *  Buffer.h
 *  PX
 *
 *  Created by Patrick Zulian on 12/22/10.
 *  
 *
 */

#ifndef NSE_SOLVER_BUFFER_H
#define NSE_SOLVER_BUFFER_H


#include "Base.h"
#include "Utils.h"
#include "Array.h"

namespace nsesolver  {
	
	template< typename T >
	class Buffer : public Array< T >{
	public:
		int offset;
		
		explicit Buffer(int size)
		: Array< T >(size), offset(0)
		{
			
		}
		
		
		Buffer(const Buffer &other)
		: Array< T >(other), offset(other.offset)
		{
			std::cout << "Buffer copy constructor called" << std::endl;
		}
		
		
		inline int getContentSize() const
		{
			return offset;
		}
		
		Buffer & operator = (const Buffer &other)
		{
			
			if (this == &other) {
				return *this;
			}
			
			Array< T >::operator=(other);
			offset = other.offset;
			
			std::cout << "Buffer copy operator called" << std::endl;
			
			return *this;
		}
		
		
		
		inline Buffer & operator << (const T &value)
		{
			NSE_SOLVER_ASSERT(offset < Array< T >::size());
			
			(*this)[offset] = value;
			offset++;
			
			return *this;
		}
		
		inline Buffer & append(T * array, int extent)
		{
			NSE_SOLVER_ASSERT(offset + extent < Array< T >::size());
			
			utils::Copy(&((*this)[offset]), array, extent);
			offset += extent;
			
			return *this;
		}
		
		inline
		void reset()
		{
			offset = 0;
		}
	};
	
}
#endif
